﻿#include "zuhechaxun.h"
#include "ui_zuhechaxun.h"
#include <QDebug>
#include <QSqlQuery>
#include <QTextCodec>
#include <QMessageBox>

Form2::Form2(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form2)
{
    ui->setupUi(this);
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF8"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF8"));
    //ui.scrollAreaWidgetContents->adjustSize();
    //connect(ui->treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), ui->treeWidget_2, SLOT(slotDoubleClickItem(QTreeWidgetItem *, int)));
    connect(ui->treeWidget_2,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(showSelectedImage(QTreeWidgetItem*,int)));
    //时间设置
    ui->renzhis->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->renzhis->setCalendarPopup(true);
    ui->renzhie->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->renzhie->setCalendarPopup(true);
    ui->births->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->births->setCalendarPopup(true);
    ui->birthe->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->birthe->setCalendarPopup(true);
    ui->daiyus->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->daiyus->setCalendarPopup(true);
    ui->daiyue->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->daiyue->setCalendarPopup(true);
    ui->rudangs->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->rudangs->setCalendarPopup(true);
    ui->rudange->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->rudange->setCalendarPopup(true);
    ui->joinworks->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->joinworks->setCalendarPopup(true);
    ui->joinworke->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->joinworke->setCalendarPopup(true);
    connect(ui->jiguansheng,SIGNAL(currentIndexChanged(QString)),this,SLOT(printjiguansheng()));

}
Form2::~Form2()
{
    delete ui;
}

//void Form2::slotDoubleClickItem(QTreeWidgetItem *item, int col)
//{
// 从item中获得QString text()或者QVariant data()
// QTreeWidgetItem *parent=item->parent();//获得父节点
// if(NULL==parent)
//     return;
//progessBar->setValue(0);//进度条置0
//int row=parent->indexOfChild(item);//获得节点在父节点中的行号(从0开始)
//QString text=parent->text(0);//获得父节点的文本字符(即影像路径)
//qDebug()<<text;
//qDebug()<<row;
/* QString->const char* */
// QByteArray ba=fileName.toLocal8Bit();
//const char* filePath=ba.data();
//QVariant data();
// QString text();

//}

//void Form2::on_treeWidget_doubleClicked(const QModelIndex &index)
//{

// QModelIndex index1=ui->treeWidget->itemFromIndex(index);
//qDebug()<<"row"<<index.topLevelItem();
//qDebug()<<"cloumn"<<index.column();
// 从item中获得QString text()或者QVariant data()
// QTreeWidgetItem *parent=item->parent();//获得父节点
// if(NULL==parent)
//     return;
//progessBar->setValue(0);//进度条置0
// int row=parent->indexOfChild(item);//获得节点在父节点中的行号(从0开始)
// QString text=parent->text(0);//获得父节点的文本字符(即影像路径)
//qDebug()<<text;
// qDebug()<<row;


//    const bool __sortingEnabled = treeWidget->isSortingEnabled();
//    treeWidget->setSortingEnabled(false);
//   QTreeWidgetItem *___qtreewidgetitem = treeWidget->topLevelItem(0);

// QTreeWidgetItem *___qtreewidgetitem1 = ___qtreewidgetitem->child(0);

//QTreeWidgetItem *___qtreewidgetitem2 = ___qtreewidgetitem->child(1);

//QTreeWidgetItem *___qtreewidgetitem3 = treeWidget->topLevelItem(1);

//    QTreeWidgetItem *___qtreewidgetitem4 = ___qtreewidgetitem3->child(0);

//    QTreeWidgetItem *___qtreewidgetitem5 = ___qtreewidgetitem3->child(1);

//    QTreeWidgetItem *___qtreewidgetitem6 = ___qtreewidgetitem3->child(2);

// QTreeWidgetItem *___qtreewidgetitem7 = treeWidget->topLevelItem(2);

//    QTreeWidgetItem *___qtreewidgetitem8 = ___qtreewidgetitem7->child(0);

//    QTreeWidgetItem *___qtreewidgetitem9 = ___qtreewidgetitem7->child(1);

//    treeWidget->setSortingEnabled(__sortingEnabled);


//}


void Form2::showSelectedImage(QTreeWidgetItem *item, int column)
{
    QTreeWidgetItem * currentItem = ui->treeWidget_2->currentItem();
    QTreeWidgetItem *parent = item->parent();
    //QTreeWidgetItem *parent = currentItem->parent();
    if(NULL==parent) //注意：最顶端项是没有父节点的，双击这些项时注意(陷阱)
        return;
    int col =parent->indexOfChild(item); //item在父项中的节点行号(从0开始)

    if(0==col) //Band1
    {
        qDebug()<<"1";
        qDebug()<<column;
        //delete ui->treeWidget_2->takeTopLevelItem(ui->treeWidget_2->currentIndex().row());//删除全部节点
        //delete ui->treeWidget_2->currentItem->parent()->takeChild(ui->treeWidget_2->currentIndex().row());
        delete currentItem->parent()->takeChild(ui->treeWidget_2->currentIndex().row());//删除当前节点
    }
    if(1==col) //Band2
    {
        qDebug()<<"2";
        qDebug()<<column;
        //delete ui->treeWidget_2->currentItem->parent->takeChild(ui->treeWidget_2->currentIndex().row());
        delete currentItem->parent()->takeChild(ui->treeWidget_2->currentIndex().row());//删除当前节点
    }
}

void Form2::on_pushButton_clicked()//连接重名干部信号槽
{
    same=new samename();
    connect(same,SIGNAL(sendData1(QString)),this,SLOT(recvData1(QString)));

    if(same->exec()==QDialog::Accepted){
    }
    delete same;
    //    same->show();


}
void Form2::recvData1(QString id)//接收重名干部
{
    emit sendData2(id);
}

void Form2::printjiguansheng(){
    if(ui->comboBox->currentIndex()==0) qDebug()<<"属于";
    else if(ui->comboBox->currentIndex()==1) qDebug()<<"河南省";
    else if(ui->comboBox->currentIndex()==2) qDebug()<<"北京市";
    else if(ui->comboBox->currentIndex()==3) qDebug()<<"吉林省";


}

void Form2::on_pushButton_3_clicked()//发送一个根据条件查询后的干部IDNum
{
    //定义数据库
    QSqlQuery query;                            //查询
    QSqlQuery changeunit;                       //查询单位
    QSqlQuery changezhiwu;                      //查询职务
    QSqlQuery selectedus;                       //查询schooledu全日制专业
    QSqlQuery selecteduw;                       //查询workwdu在职专业
    QSqlQuery selectbirthtime1;                 //查询出生时间，用于出生日起查询
    QSqlQuery selectbirthtime2;                 //查询出生时间，用于年龄查询

    //需要先把查询的字符串定义为“NULL“，否则查询容易程序异常结束
    QString iddate="NULL";
    QString name;
    QString unitname;
    QString unitid="NULL";
    QString zhiwuname;
    QString schooledu;
    QString workedu;
    QString jiguan="NULL";
    QString jiguansheng;
    QString jiguanshi;
    QDate births;                       //出生日期start
    QDate birthe;                       //出生日期end
    QString oldmax;                     //最大年龄
    QString oldmin;                     //最小年龄
    QDate birthtime;


    //***********查询学历表中的干部在职和全日制专业（在职0，全日1）选择干部的IDNum****************
    schooledu=ui->schooledu->text();//此处是专业的名字
    selectedus.exec(QString("select IDNum from Edu where Major='%1' and WorkORSchool='%2'").arg(schooledu).arg(1));
    while(selectedus.next()){
        iddate=selectedus.value(0).toString();
        emit sendData3(iddate);//发送查询的干部IDNum
        this->close();
    }
    workedu=ui->workedu->text();//此处是专业的名字
    selecteduw.exec(QString("select IDNum from Edu where Major='%1' and WorkORSchool='%2'").arg(schooledu).arg(0));
    while(selecteduw.next()){
        iddate=selecteduw.value(0).toString();
        emit sendData3(iddate);//发送查询的干部IDNum
        this->close();
    }
    //****************************************************************************************
//    qDebug()<<"3333"<<iddate;

    //****************职务需要查询职务表，选择现任职务的干部IDNum和现任类型=0，输出职务名字***************
    //职务需要单独查询
    //不同表取了多个相同的IDNum如何只取一个IDNum
    zhiwuname=ui->zhiwu->text();//职务查询
    changezhiwu.exec(QString("select IDNum from Post where PostName='%1'and QualifyType='%2'").arg(zhiwuname).arg(0));
    while(changezhiwu.next()){
        iddate=changezhiwu.value(0).toString();
        emit sendData3(iddate);//发送查询的干部IDNum
        this->close();
    }
    //***************************************************************************************
//    qDebug()<<"22"<<iddate;
    //**************************在干部信息表中查询******************************************
    //把单位名称切换成单位的ID，用于在干部信息表查询
    unitname=ui->unit->text();//注意输入的是单位的名字！！！！！！！！！！！！！
    changeunit.exec(QString("select unitid from unit where unitname='%1'").arg(unitname));
    while(changeunit.next()){
        unitid=changeunit.value(0).toString();
    }
    //查询籍贯的信息，用于在干部信息表查询
    //jiguansheng=ui->jiguansheng->text();//取comboBox(jiguansheng)的值？？？？？？？？？？
    jiguansheng="吉林省";
    jiguanshi=ui->jiguanshi->text();
    jiguan=jiguansheng+jiguanshi;





    //出生时间查询
    //     births;//出生日期start
    //     birthe;//出生日期end
    births=ui->births->date();
    birthe=ui->birthe->date();


    //分解时间的年月日
    int sbiry=births.year();
    int sbirm=births.month();
    int sbird=births.day();

    int ebiry=birthe.year();
    int ebirm=birthe.month();
    int ebird=birthe.day();
    //    qDebug()<<sbiry;
    //    qDebug()<<sbirm;
    //    qDebug()<<sbird;
    //    qDebug()<<ebiry;
    //    qDebug()<<ebirm;
    //    qDebug()<<ebird;
    if(sbiry!=1900 || sbirm!=1 || sbird!=1 || ebiry!=1900 || ebirm!=1 || ebird!=1){



        selectbirthtime1.exec("select Birth,IDNum from CadreInfo ");
        while(selectbirthtime1.next()){
            birthtime=selectbirthtime1.value(0).toDate();
            iddate=selectbirthtime1.value(1).toString();
            int y=birthtime.year();
            int m=birthtime.month();
            int d=birthtime.day();
            if(y<ebiry && y>sbiry){//年份在两个之间，直接传iddate
                emit sendData3(iddate);//发送查询的干部IDNum
                this->close();
                //            qDebug()<<y;

            }
            else if(y==ebiry){//年份正好与截止时间相同，判断月份,月份仅与截止时间的月份相比
                if(m<ebirm){//小于截止月份时间
                    emit sendData3(iddate);//发送查询的干部IDNum
                    this->close();
                }
                else if(m==ebirm){//等于截止时间，判断day，仅与截止day相比
                    if(d<ebird && d==ebird){//小于截止day
                        emit sendData3(iddate);//发送查询的干部IDNum
                        this->close();
                    }
                }


            }
            else if(y==sbiry){//年份正好与起始时间相同
                if(m>sbirm){//大于起始月份时间
                    emit sendData3(iddate);//发送查询的干部IDNum
                    this->close();
                }
                else if(m==ebirm){//等于起始时间，判断day，仅与起始day相比
                    if(d>sbird && d==sbird){//大于或者等于起始day
                        emit sendData3(iddate);//发送查询的干部IDNum
                        this->close();
                    }
                }

            }

        }

    }

//    qDebug()<<"1111"<<iddate;
    //    }

    //年龄查询
    //当前时间的年份-最小年龄=出生年份的上限
    //当前时间的年份-最大年龄=出生年份的
    //下限<出生年份<上限
    QDate year;
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date =current_date_time.toString("yyyy");
    //    qDebug()<<current_date;
    int date = current_date.toInt();

    oldmax=ui->oldmax->text();//最大年龄
    oldmin=ui->oldmin->text();//最小年龄

    if(oldmax!="NULL"|| oldmin!="NULL"){

        int oldbig=oldmax.toInt();
        int oldsmall=oldmin.toInt();
        //    qDebug()<<date;
        //    qDebug()<<oldbig;
        //    qDebug()<<oldsmall;

        int minyear=date-oldbig;
        int maxyear=date-oldsmall;
        //    qDebug()<<minyear;
        //    qDebug()<<maxyear;
        selectbirthtime2.exec("select Birth,IDNum from CadreInfo ");
        while(selectbirthtime2.next()){
            year=selectbirthtime2.value(0).toDate();
            iddate=selectbirthtime2.value(1).toString();
            int y=year.year();
            if(y<maxyear || y>minyear){
                emit sendData3(iddate);//发送查询的干部IDNum
                this->close();
                //            qDebug()<<y;
            }
        }
    }
//    qDebug()<<"111"<<iddate;
    //姓名、单位、籍贯查询
    name=ui->name->text();
    query.exec(QString("select IDNum from CadreInfo where CadreName ='%1' or UnitID='%2' or NativePlace='%3'").arg(name).arg(unitid).arg(jiguan));//查询或关系
    while(query.next())
    {
        iddate=query.value(0).toString();
        emit sendData3(iddate);//发送查询的干部IDNum
        this->close();
    }
    //*****************************************************************************************
//    qDebug()<<"11"<<iddate;

    if(iddate=="NULL"){
        QMessageBox::information(this, tr("提示信息"), tr("系统中未找到查询干部的信息"));
    }
}


